import request from "@/utils/request";

export default {
    logout:logout,
    getToken: getToken,
    setToken: setToken,
    getAccount: getAccount,
    setAccount: setAccount,
    fetchMenus: fetchMenus,
    clearMenus: clearMenus,
}

const TokenKey = 'WorkflowToken'
const GlobalAccountKey = 'WorkflowGlobalAccountKey'

let _globalAccount = {}

export function logout() {
    localStorage.removeItem(TokenKey)
    localStorage.removeItem(GlobalAccountKey)
    _globalAccount = {}
}

export function getAccount() {
    if (!_globalAccount.id) {
        let s = localStorage.getItem(GlobalAccountKey)
        let v = JSON.parse(s)
        if (v && v.id) {_globalAccount = v}
    }
    return _globalAccount
}


export function setAccount(info) {
    _globalAccount = info
    localStorage.setItem(GlobalAccountKey, JSON.stringify(info))
    return _globalAccount
}


export async function fetchMenus() {
    let token = getToken();
    if (token) {
        await request.get({
            url:"user.menus",
            data: {}
        }).then(res => {
            _globalAccount.menus = formatMenus(res.result)
        })
    }
}


export async function clearMenus(sys) {
    if (_globalAccount) {_globalAccount.menus = []}
}


export function getToken() {
    return localStorage.getItem(TokenKey)
}

export function setToken(token) {
    if (!token || token === "") {
        localStorage.removeItem(TokenKey)
        return
    }
    localStorage.setItem(TokenKey, token)
}

const _import = comp => require(`@/${comp}`).default
function formatMenus(menus) {
    if (!menus || menus.length === 0) {return []}
    let res = []
    for (let i = 0; i < menus.length; i++) {
        let m1 = menus[i]
        let v1 = {
            path: m1.path,
            name: m1.path,
            component: _import(m1.component),
            hidden: m1.hidden,
            meta: { title: m1.title, icon:m1.icon },
            children: []
        }
        if (m1.children && m1.children.length > 0) {
            for (let j = 0; j < m1.children.length; j++) {
                let m2 = m1.children[j]
                let v2 = {
                    path: m2.path,
                    name: m2.path,
                    component: _import(m2.component),
                    hidden: m2.hidden,
                    meta: { title: m2.title, icon:m2.icon },
                    children: []
                }
                if (m2.children && m2.children.length > 0) {
                    for (let k = 0; k < m2.children.length; k++) {
                        let m3 = m2.children[k]
                        let v3 = {
                            path: m3.path,
                            name: m3.path,
                            component: _import(m3.component),
                            hidden: m3.hidden,
                            meta: { title: m3.title, icon:m3.icon }
                        }
                        v2.children.push(v3)
                    }
                }
                v1.children.push(v2)
            }
        }
        res.push(v1)
    }
    return res
}
